﻿Imports DevExpress.XtraBars
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.XtraGrid

Public Class ModAdm_Adjuntos

    Private Sub ModAdm_Adjuntos_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        PopupMenu1.ItemLinks.Add(New BarButtonItem(BarManager1, "Eliminar adjunto"))
        SetUpGrid(GridControl1)
        npMain.Text = "Es recomendable que los archivos adjuntos sean documentos en PDFS para su fácil manipulación."
    End Sub


    Private Sub GridView1_PopupMenuShowing(sender As Object, e As DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs) Handles GridView1.PopupMenuShowing
        Dim View As GridView = CType(sender, GridView)
        Dim HitInfo As GridHitInfo
        HitInfo = View.CalcHitInfo(e.Point)
        If HitInfo.InRowCell Then
            View.FocusedRowHandle = HitInfo.RowHandle
            PopupMenu1.ShowPopup(BarManager1, View.GridControl.PointToScreen(e.Point))
        End If
    End Sub


    Public Sub SetUpGrid(ByVal grid As GridControl)
        grid.AllowDrop = True
        AddHandler grid.DragOver, AddressOf grid_DragOver
        AddHandler grid.DragDrop, AddressOf grid_DragDrop
        Dim view As GridView = TryCast(grid.MainView, GridView)
        view.OptionsBehavior.Editable = False
        AddHandler view.MouseMove, AddressOf view_MouseMove
        AddHandler view.MouseDown, AddressOf view_MouseDown
    End Sub

    Private downHitInfo As GridHitInfo = Nothing

    Private Sub view_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim view As GridView = TryCast(sender, GridView)
        downHitInfo = Nothing
        Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
        If Control.ModifierKeys <> Keys.None Then
            Return
        End If
        If e.Button = MouseButtons.Left AndAlso hitInfo.RowHandle >= 0 Then
            downHitInfo = hitInfo
        End If
    End Sub

    Private Sub view_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim view As GridView = TryCast(sender, GridView)
        If e.Button = MouseButtons.Left AndAlso downHitInfo IsNot Nothing Then
            Dim dragSize As Size = SystemInformation.DragSize
            Dim dragRect As New Rectangle(New Point(downHitInfo.HitPoint.X - dragSize.Width / 2, downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize)
            If (Not dragRect.Contains(New Point(e.X, e.Y))) Then
                Dim row As DataRow = view.GetDataRow(downHitInfo.RowHandle)
                view.GridControl.DoDragDrop(row, DragDropEffects.Move)
                downHitInfo = Nothing
                DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = True
            End If
        End If
    End Sub

    Private Sub grid_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(GetType(DataRow)) Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub grid_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        Dim grid As GridControl = TryCast(sender, GridControl)
        Dim table As DataTable = TryCast(grid.DataSource, DataTable)
        Dim row As DataRow = TryCast(e.Data.GetData(GetType(DataRow)), DataRow)
        If row IsNot Nothing AndAlso table IsNot Nothing AndAlso row.Table IsNot table Then
            table.ImportRow(row)
            'row.Delete()
        End If
    End Sub

    Private Sub SetDefaultLabel()
        label1.ImageIndex = 1
        SetDefaultCursor()
    End Sub


    Private Sub SetDefaultCursor()
        Cursor = Cursors.Default
    End Sub

    Private Sub label1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles label1.DragEnter
        label1.ImageIndex = 0
    End Sub

    Private Sub label1_DragLeave(sender As Object, e As System.EventArgs) Handles label1.DragLeave
        label1.ImageIndex = 0
    End Sub

    Private Sub label1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles label1.DragOver
        e.Effect = DragDropEffects.Move
        SetDefaultLabel()
    End Sub
End Class
